//
//  HDLoadingView.m
//  AccountEasy
//
//  Created by 黄志刚 on 2020/8/28.
//  Copyright © 2020 AccountEasy. All rights reserved.
//

#import "HDLoadingView.h"

@interface HDLoadingView ()
@property (nonatomic,strong) UIImageView *lodadingiconView;
@property (nonatomic,strong) UILabel *messageLable;

@end
@implementation HDLoadingView
- (void)setMessage:(NSString *)message {
    _message = message;
    if (message.length > 0) {
        self.messageLable.text = _message;
        return;
    }
    self.messageLable.hidden = YES;
    [self.lodadingiconView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(24);
        make.right.mas_equalTo(-24);
        make.width.mas_equalTo(self.lodadingiconView.mas_height);
        make.centerY.mas_equalTo(self.mas_centerY);
    }];
}
- (void)awakeFromNib
{
    [super awakeFromNib];
    if ([self respondsToSelector:@selector(initView)]) {
        [self initView];
    }
    
}

- (id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        if ([self respondsToSelector:@selector(initView)]) {
            [self initView];
        }
    }
    return self;
}

- (void)initView{
    
    [self setupUI];
    [self setupConstraint];
}

- (void)setupUI {
    UIImage *image = [[UIImage imageNamed:@"hub_lodading_bg"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
    self.image = image;
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectZero];
    bgView.backgroundColor = [UIColor whiteColor];
    bgView.layer.cornerRadius = 3;
    [self addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(6);
        make.right.mas_equalTo(-6);
        make.top.mas_equalTo(6);
        make.bottom.mas_equalTo(-6);
    }];
    [self addSubview:self.lodadingiconView];
    [self addSubview:self.messageLable];
}
- (void)setupConstraint {
    
    [self.lodadingiconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(24);
        make.right.mas_equalTo(-24);
        make.width.mas_equalTo(self.lodadingiconView.mas_height);
        make.bottom.mas_equalTo(self.mas_bottom).mas_offset(-30);
    }];
    
    [self.messageLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(5);
        make.right.mas_equalTo(-5);
        make.bottom.mas_equalTo(self.mas_bottom).mas_offset(-10);
    }];
    
}
- (UIImageView *)lodadingiconView {
    if (!_lodadingiconView) {
        _lodadingiconView = [[UIImageView alloc] initWithFrame:CGRectZero];
        _lodadingiconView.image = [UIImage imageNamed:@"hub_lodading_icon"];
        CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
        animation.duration = 2.0f; // 持续时间
        animation.repeatCount = HUGE_VALF; // 重复次数
        animation.fromValue = [NSNumber numberWithFloat:0.0]; // 起始角度
        animation.toValue = [NSNumber numberWithFloat: 6 * M_PI]; // 终止角度
        animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
        // 添加动画
        [_lodadingiconView.layer addAnimation:animation forKey:@"rotate-layer"];
    }
    return _lodadingiconView;
}
- (UILabel *)messageLable {
    if (!_messageLable) {
        _messageLable = [[UILabel alloc] initWithFrame:CGRectZero];
        [_messageLable setFont:kFont(12)];
        [_messageLable setTextColor:colorHex(0x13334D)];
        _messageLable.textAlignment = NSTextAlignmentCenter;
    }
    return _messageLable;
}

@end
